﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ninject;
using Legazpi.Payroll.BusinessLogic.Services;
using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
using Legazpi.Payroll.DataAccess.DataObjects;
using Web.Codes;

namespace Legazpi.Payroll.Web.Admin
{
    public partial class Users : BasePage
    {
        [Inject]
        public IUserService _usersService { get; set; }

        public bool IsSuccess { get; set; }
        public string NotifyText { get; set; }

        #region Implementations
        void BindGrid()
        {
            repeater.DataSource = _usersService.GetAll();
            repeater.DataBind();
        }
        void Delete(int id)
        {
            _usersService.Delete(id);
            IsSuccess = _usersService.Save();
            NotifyText = "Successfully Deleted.";
            BindGrid();
        }

        void Edit(int id)
        {
            var user = _usersService.GetById(id);
            if (user == null)
                return;          

            ObjectToData(user);

            UserId.Value = user.Id.ToString();            
            SubmitBtn.Text = "Update";
        }

        void Update(int id)
        {
            var user = _usersService.GetById(id);
            if (user == null)
            {
                Add();
                return;
            }
            DataToObject(ref user);

            _usersService.Update(user);
            IsSuccess = _usersService.Save();
            NotifyText = "Successfully Updated Record.";
        }
        void Add()
        {
            var user = new User();

            DataToObject(ref user);

            _usersService.Add(user);
            IsSuccess = _usersService.Save();

            UserId.Value = user.Id.ToString();
            NotifyText = "Successfully Added New Record.";            
            SubmitBtn.Text = "Update";

        }
        void ClearForm()
        {
            ClearControl(this.Page);
            UserId.Value = "0";
            SubmitBtn.Text = "Submit";
            PasswordTxt.Attributes.Add("value",string.Empty);
            ConifrmPassTxt.Attributes.Add("value", string.Empty);   
        }
        void ClearControl(Control ctrl)
        {
            foreach (Control _ctrl in ctrl.Controls)
            {
                if (_ctrl.GetType() == typeof(TextBox))
                {
                    ((TextBox)_ctrl).Text = string.Empty;
                }
                else if (_ctrl.GetType() == typeof(DropDownList))
                {
                    ((DropDownList)_ctrl).SelectedIndex = 0;
                }
                ClearControl(_ctrl);
            }
        }
        void Save()
        {
            int empId;
            int.TryParse(UserId.Value, out empId);
            if (empId == 0)
                Add();
            else
                Update(empId);
        }
        void DataToObject(ref User obj)
        {
            obj.UserName = UserNameTxt.Text;
            obj.Password = PasswordTxt.Text;
            obj.Email = EmailTxt.Text ;
        }

        void ObjectToData(User obj)
        {
            UserNameTxt.Text = obj.UserName;
            EmailTxt.Text = obj.Email;
            PasswordTxt.Attributes.Add("value", obj.Password);
            ConifrmPassTxt.Attributes.Add("value", obj.Password);            
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                BindGrid();
        }

        protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int intTemp = 0;
            if (int.TryParse((string)e.CommandArgument, out intTemp))
            {
                switch (e.CommandName.ToLower())
                {
                    case Constants.Update_Command:
                        Edit(intTemp);
                        break;
                    case Constants.Delete_Command:
                        Delete(intTemp);
                        break;
                }
            }
        }

        protected void SubmitBtn_Click(object sender, EventArgs e)
        {
            Save();
            if (IsSuccess)
            {
                ClearForm();
                BindGrid();
            }                
        }

        protected void CancelBtn_Click(object sender, EventArgs e)
        {
            ClearForm();
        }
    }
}